
const db = uniCloud.database();

export function compareTimestamp(timestamp) {
	const currentTime = new Date().getTime();
	const timeDiff = currentTime - timestamp;

	if (timeDiff < 60000) {
		return '1分钟内';
	} else if (timeDiff < 3600000) {
		return Math.floor(timeDiff / 60000) + '分钟';
	} else if (timeDiff < 86400000) {
		return Math.floor(timeDiff / 3600000) + '小时';
	} else if (timeDiff < 2592000000) {
		return Math.floor(timeDiff / 86400000) + '天';
	} else if (timeDiff < 7776000000) {
		return Math.floor(timeDiff / 2592000000) + '月';
	} else {
		return null;
	}
}


//提示框
export function showToast({
	title = "",
	duration = 1500,
	icon = "none",
	mask = false
} = {}) {
	uni.showToast({
		title: String(title),
		icon,
		duration,
		mask
	})
}

export function gotoHome() {
	uni.showModal({
		title: "提示",
		content: "页面有误将返回首页",
		showCancel: false,
		success: (res) => {
			if (res.confirm) {
				uni.reLaunch({
					url: "/pages/index/index"
				})
			}
		}
	})
}



export function getUserOpenId() {
	return new Promise((resolve, reject) => {
		const openid = uni.getStorageSync("UserOpenId");
		if (openid !== undefined && openid !== "") {
			console.log("获取到openid:", openid);
			resolve(openid);
		} else {
			uni.login({
				provider: "weixin",
				success: (res) => {
					if (res !== undefined && res.code) {
						uniCloud.callFunction({
							name: "getOpenId",
							data: {
								code: res.code
							}
						}).then(res => {
							console.log("获取到openid:", JSON.stringify(res));
							if (res !== undefined && res.result !== undefined) {
								uni.setStorage({
									data: res.result,
									key: "UserOpenId",
									success: () => {},
								});
								resolve(res.result);
							}
						})
					}
				},
				fail(e) {
					console.log("获取openid异常:", JSON.stringify(e));
					reject(e);
				}
			});
		}
	});
}

